/**
*      author: Y_Yao
*      created: 2024-11-25 10:39:20
**/

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 7;
int a[N];
int n;
int cnt[N];

int count(int start)
{
    memset(cnt,0,sizeof cnt);
    int r = start;
    int ans = 0;
    for(int l = r;l <= n;l += 2)
    {
        while(r <= n - 1 && a[r] == a[r + 1] && cnt[a[r]] == 0)
        {
            cnt[a[r]] += 2;
            r += 2;
        }
        ans = max(ans,r - l);
        if(r >= l + 2) cnt[a[l]] -= 2;
        else r = l + 2;
    }

    return ans;
}

void solve()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
    }

    int ans1 = count(1);
    int ans2 = count(2);
    cout << max(ans1,ans2) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}